<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col
              :span="6"
              v-hasPermi="['bpm:manager:reports:city:districtId']"
            >
              <el-form-item label="区域" prop="districtId">
                <el-select
                  v-model="queryParams.districtId"
                  placeholder="请选择"
                  clearable
                  filterable
                  @change="handleQuery"
                >
                  <el-option
                    v-for="item in AreaData"
                    :key="item.districtId"
                    :label="item.districtName"
                    :value="item.districtId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="门店" prop="storeIds">
                <el-select
                  v-model="queryParams.storeIds"
                  placeholder="请选择"
                  filterable
                  clearable
                  multiple
                  collapse-tags
                  class="w-300"
                >
                  <el-option
                    v-for="(item, index) in $store.state.user.stores"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <div class="timebox">
                <el-form-item label="日期时间" prop="date">
                  <el-date-picker
                    v-model="queryParams.date"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :picker-options="pickerOptions"
                    @change="minDate = ''"
                  >
                  </el-date-picker>

                  <!-- <el-date-picker
                    type="dates"
                    v-model="queryParams.date"
                    placeholder="选择一个或多个日期"
                    clas
                  >
                  </el-date-picker> -->
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left1"></span>
        <div class="table_card_title">城市经理报表</div>
        <div class="op_right">
          <el-button
            @click="handleExport"
            :loading="exportLoading"
            class="repeat"
            v-hasPermi="['order:finance:ly:city:exl']"
            >导出</el-button
          >
        </div>
      </div>

      <div class="table_list">
        <el-table
          :data="CityManagerList"
          height="calc(100vh - 300px)"
          @sort-change="handleSort"
        >
          <el-table-column
            prop="districtName"
            label="区域"
            align="center"
            width="200"
          >
          </el-table-column>
          <el-table-column
            prop="storeMtId"
            label="美团ID"
            align="center"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="storeDyId"
            label="抖音ID"
            align="center"
            width="150"
          >
          </el-table-column>
          <el-table-column
            prop="storeName"
            label="门店名称"
            align="center"
            width="170"
            fixed="left"
          >
          </el-table-column>

          <el-table-column label="业绩任务" align="center" prop="taskMoney">
          </el-table-column>

          <el-table-column
            label="总业绩"
            align="center"
            prop="soMoney"
            sortable="custom"
            width="130px"
          >
          </el-table-column>

          <el-table-column
            label="应完成业绩"
            align="center"
            prop="wantSoMoney"
            width="140"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="业绩达成率"
            align="center"
            prop="soPro"
            width="150"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="客量任务"
            align="center"
            prop="taskCust"
            width="130"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="总客量"
            align="center"
            prop="totalCust"
            sortable="custom"
            width="130px"
          >
          </el-table-column>

          <el-table-column
            label="应完成客量"
            align="center"
            prop="totalCustWant"
            width="150"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="客量达成率"
            align="center"
            prop="custPro"
            width="150"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="总充卡"
            align="center"
            prop="rechargeMoneyTotal"
            sortable="custom"
            width="160px"
          >
          </el-table-column>

          <el-table-column
            label="新客占比"
            align="center"
            prop="newCustProportion"
            sortable="custom"
            width="140px"
          >
          </el-table-column>

          <el-table-column
            label="会员占比"
            align="center"
            prop="cardProportion"
            sortable="custom"
            width="140px"
          >
          </el-table-column>
          <el-table-column
            label="日均"
            align="center"
            prop="dayMoneyAverage"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="加钟"
            align="center"
            prop="addClock"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="加钟率"
            align="center"
            prop="addClockProportion"
            sortable="custom"
            width="140px"
          >
          </el-table-column>
          <el-table-column
            label="点钟"
            align="center"
            prop="pointClock"
            sortable="custom"
          >
          </el-table-column>

          <el-table-column
            label="点钟率"
            align="center"
            prop="pointClockProportion"
            sortable="custom"
            width="140px"
          >
          </el-table-column>
          <el-table-column
            label="客单价"
            align="center"
            prop="customerUnitPrice"
            sortable="custom"
            width="130px"
          >
          </el-table-column>

          <!-- <el-table-column
            label="美团营业额占比"
            align="center"
            prop="mtSoMoneyProportionl"
            width="150"
            sortable="custom"
          >
          </el-table-column>
          <el-table-column
            label="美团充卡占比"
            align="center"
            prop="mtRechargeMoneyProportion"
            sortable="custom"
            width="150"
          >
          </el-table-column> -->

          <el-table-column
            label="抖音营业额"
            align="center"
            prop="dyMoney"
            width="150"
            sortable="custom"
          >
          </el-table-column>
          <el-table-column
            label="美团充卡"
            align="center"
            prop="mtRechanrgMoney"
            sortable="custom"
            width="150"
          >
          </el-table-column>
          <el-table-column
            label="美团营业金额"
            align="center"
            prop="mtSoMoney"
            sortable="custom"
            width="150px"
          >
          </el-table-column>
          <el-table-column
            label="充卡占比"
            align="center"
            prop="rechargeCardProportion"
            sortable="custom"
            width="150px"
          >
          </el-table-column>
        </el-table>
        <!-- <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        /> -->
      </div>
    </div>
  </div>
</template>
        
  <script>
import { getProfitCityExport } from "@/api/bpm/excle";
import { getFinanceCity } from "@/api/bpm/reports";
import timer from "@/components/getTime/timer";
import { monthDays } from "@/utils/utils";
import { dayDataTime } from "@/utils/utils";
import { getManagerDistrict } from "@/api/bpm/commission";
export default {
  name: "Storeadmincitymanagerreport",
  components: { timer },
  data() {
    return {
      loading: false,
      exportLoading: false,
      saveLoading: false,
      value: "",
      queryParams: {
        //   pageNo: 1,
        //   pageSize: 10,
        storeIds: [],
        date: [],
        startDate: "",
        endDate: "",
        districtId: "",
        field: "",
        sortType: "",
      },
      AreaData: [], // 区域数据
      CityManagerList: [],
      total: 0,
      exportLoading: false,

      minDate: "",
    };
  },

  computed: {
    pickerOptions() {
      let _this = this;
      return {
        disabledDate(time) {
          if (!!_this.minDate) {
            // console.log("aa", _this.minDate);
            const date = new Date(_this.minDate);
            const month =
              date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1; // 获取月
            const year = date.getFullYear(); // 获取年
            let moon = monthDays(year + "-" + month);
            let strTime = year + "-" + month + "-01"; // 此月起始时间
            let endTime = year + "-" + month + "-" + moon; // 此月结束时间
            // console.log(strTime);
            // console.log(endTime);
            // console.log("ss",time);
            return !(
              time.getTime() >= new Date(strTime + " 00:00:00").getTime() &&
              time.getTime() < new Date(endTime).getTime()
            );
          }
        },
        onPick({ maxDate, minDate }) {
          console.log(minDate);

          if (!maxDate) {
            _this.minDate = minDate;
          }
        },
      };
    },
  },

  mounted() {},

  created() {
    const date = dayDataTime("obj");
    console.log(date);
    let startDate = date.y + "-" + date.m + "-01";
    let endDate = date.str;
    this.queryParams.date = [startDate, endDate];
    this.getList();
    this.getAreaData();
  },

  methods: {
    handleSort({ column, prop, order }) {
      if (column.order === "ascending") {
        this.queryParams.sortType = "ASC";
      } else if (column.order === "descending") {
        this.queryParams.sortType = "DESC";
      }
      this.queryParams.field = column.property;
      this.getList();
    },
    // 获取区域数据
    getAreaData() {
      getManagerDistrict(this.queryParams)
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.AreaData = data;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    handleExport() {
      this.$modal
        .confirm("是否确认导出报表数据?")
        .then(() => {
          this.exportLoading = true;
          let queryParams = JSON.parse(JSON.stringify(this.queryParams));

          if (!!this.queryParams.date) {
            queryParams.startDate = this.queryParams.date[0];
            queryParams.endDate = this.queryParams.date[1];
          }
          delete queryParams.date;
          return getProfitCityExport(queryParams);
        })
        .then((response) => {
          this.$download.excel(response, "城市经理报表");
          this.exportLoading = false;
        })
        .catch(() => {
          this.exportLoading = false;
        });
    },

    // 筛选
    handleQuery() {
      // this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
    },

    // 城市经理
    getList() {
      this.loading = true;

      // if (
      //   !!this.queryParams.storeIds &&
      //   this.queryParams.storeIds.length === 0
      // ) {
      //   this.$message.error("门店不能为空");
      //   return false;
      // }

      if (!this.queryParams.date) {
        this.$message.error("日期时间不能为空");
        return false;
      }

      if (this.queryParams.date.length === 0) {
        this.$message.error("日期时间不能为空");
        return false;
      }

      const query = JSON.parse(JSON.stringify(this.queryParams));

      if (!!query.date) {
        query.startDate = this.queryParams.date[0];
        query.endDate = this.queryParams.date[1];
        delete query.date;
      }

      console.log("query", query);
      getFinanceCity(query)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.CityManagerList = data;
            // this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },
  },
};
</script>
        
  <style lang="scss" scoped>
.solid_left1 {
  margin-left: 24px;
}
.goodTop {
  width: 100%;
  height: 48px;
  background: #ffffff;
  display: flex;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.6);
  line-height: 48px;
  padding-left: 16px;
  .good_list {
    margin-right: 32px;
  }

  .good_active {
    color: #0052d9;
    border-bottom: 1px solid #0052d9;
  }
}

.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.Order_box {
  display: flex;
  margin-left: -50px;
  .select_ipt {
    width: 130px;
    margin-right: 10px;
  }
}

.pay_ipt {
  margin-left: 50px;
}

.order_ipt {
  margin-left: -120px;
}

.timebox {
  margin-top: 10px;
}

.table_list {
  padding-bottom: 24px;

  .red {
    color: red;
  }
  .green {
    color: green;
  }
  .blue {
    color: blue;
  }
}
</style>